using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace AdHocReports
{
    public partial class step5 : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MasterPage.NextEnabled = true;
            MasterPage.BackEnabled = true;
            MasterPage.CancelEnabled = true;
            MasterPage.NextClicked += new EventHandler(mp_NextClicked);
            MasterPage.BackClicked += new EventHandler(mp_BackClicked);
            MasterPage.CancelClicked += new EventHandler(mp_CancelClicked);
            MasterPage.ContentTitle = "Create a Report: Group Sub Totals";

            if (!IsPostBack && !IsCallback)
            {
                foreach (string column in ReportDefinition.SelectColumns)
                {
                    if (ReportDefinition.GroupColumns.IndexOf(column) == -1)
                    {
                        lstCols.Items.Add(column);
                    }
                }

                trSum.Visible = true;
                trAvg.Visible = false;
                trMax.Visible = false;
                trMin.Visible = false;
                trCount.Visible = false;

                lstSum.Items.Clear();
                lstAvg.Items.Clear();
                lstMin.Items.Clear();
                lstMax.Items.Clear();
                lstCount.Items.Clear();

                foreach (string sumColumn in ReportDefinition.SumColumns)
                {
                    lstSum.Items.Add(new ListItem(sumColumn, sumColumn));
                }

                foreach (string avgColumn in ReportDefinition.AvgColumns)
                {
                    lstAvg.Items.Add(new ListItem(avgColumn, avgColumn));
                }

                foreach (string minColumn in ReportDefinition.MinColumns)
                {
                    lstMin.Items.Add(new ListItem(minColumn, minColumn));
                }

                foreach (string maxColumn in ReportDefinition.MaxColumns)
                {
                    lstMax.Items.Add(new ListItem(maxColumn, maxColumn));
                }

                foreach (string countColumn in ReportDefinition.CountColumns)
                {
                    lstCount.Items.Add(new ListItem(countColumn, countColumn));
                }
            }
        }

        void mp_CancelClicked(object sender, EventArgs e)
        {
            Response.Redirect("~/");
        }

        void mp_BackClicked(object sender, EventArgs e)
        {
            UpdateAggregateColumns();

            Response.Redirect("step4.aspx");
        }

        void mp_NextClicked(object sender, EventArgs e)
        {
            UpdateAggregateColumns();

            Response.Redirect("step6.aspx");
        }

        private void UpdateAggregateColumns()
        {
            ReportDefinition.SumColumns.Clear();

            for (int i = 0; i < lstSum.Items.Count; i++)
            {
                ReportDefinition.SumColumns.Add(lstSum.Items[i].Text);
            }

            ReportDefinition.AvgColumns.Clear();

            for (int i = 0; i < lstAvg.Items.Count; i++)
            {
                ReportDefinition.AvgColumns.Add(lstAvg.Items[i].Text);
            }

            ReportDefinition.MaxColumns.Clear();

            for (int i = 0; i < lstMax.Items.Count; i++)
            {
                ReportDefinition.MaxColumns.Add(lstMax.Items[i].Text);
            }

            ReportDefinition.MinColumns.Clear();

            for (int i = 0; i < lstMin.Items.Count; i++)
            {
                ReportDefinition.MinColumns.Add(lstMin.Items[i].Text);
            }

            ReportDefinition.CountColumns.Clear();

            for (int i = 0; i < lstCount.Items.Count; i++)
            {
                ReportDefinition.CountColumns.Add(lstCount.Items[i].Text);
            }
        }

        protected void cboOps_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Check what aggregate operation was selected
            switch (cboOps.SelectedIndex)
            {
                case 0:
                    // Show SUM, hide all others
                    trSum.Visible = true;
                    trAvg.Visible = false;
                    trMax.Visible = false;
                    trMin.Visible = false;
                    trCount.Visible = false;

                    break;
                case 1:
                    // Show AVG, hide all others
                    trSum.Visible = false;
                    trAvg.Visible = true;
                    trMax.Visible = false;
                    trMin.Visible = false;
                    trCount.Visible = false;

                    break;
                case 2:
                    // Show MAX, hide all others
                    trSum.Visible = false;
                    trAvg.Visible = false;
                    trMax.Visible = true;
                    trMin.Visible = false;
                    trCount.Visible = false;

                    break;
                case 3:
                    // Show MIN, hide all others
                    trSum.Visible = false;
                    trAvg.Visible = false;
                    trMax.Visible = false;
                    trMin.Visible = true;
                    trCount.Visible = false;

                    break;
                case 4:
                    // Show COUNT, hide all others
                    trSum.Visible = false;
                    trAvg.Visible = false;
                    trMax.Visible = false;
                    trMin.Visible = false;
                    trCount.Visible = true;

                    break;
            }
        }

        protected void cmdMoveRightSum_Click(object sender, EventArgs e)
        {
            // Loop through the list of available columns
            for (int i = 0; i < lstCols.Items.Count; i++)
            {
                // If the current item is selected
                if (lstCols.Items[i].Selected == true)
                {
                    // Check the data type
                    switch (ReportDefinition.GetColumnDataType(lstCols.Items[i].Text))
                    {
                        // Numeric data types
                        case "System.Decimal":
                        case "System.Double":
                        case "System.Int16":
                        case "System.Int32":
                        case "System.Int64":
                        case "System.Single":
                        case "System.UInt16":
                        case "System.UInt32":
                        case "System.UInt64":
                            // Add the current item to SUM
                            lstSum.Items.Add(lstCols.Items[i]);
                            // Stop processing after the first item
                            break;
                    }
                }
            }

            // Loop backwards through the list of available columns
            for (int i = (lstCols.Items.Count - 1); i >= 0; i--)
            {
                // If the current item is selected
                if (lstCols.Items[i].Selected == true)
                {
                    // Check the data type
                    switch (ReportDefinition.GetColumnDataType(lstCols.Items[i].Text))
                    {
                        // Numeric data types
                        case "System.Decimal":
                        case "System.Double":
                        case "System.Int16":
                        case "System.Int32":
                        case "System.Int64":
                        case "System.Single":
                        case "System.UInt16":
                        case "System.UInt32":
                        case "System.UInt64":
                            // Remove the current item from the available list
                            lstCols.Items.Remove(lstCols.Items[i]);
                            // Stop processing after the first item
                            break;
                    }
                }
            }

            // Loop through the list of SUM columns
            for (int i = 0; i < lstSum.Items.Count; i++)
            {
                // Ensure the current item is not selected
                lstSum.Items[i].Selected = false;
            }

            UpdateAggregateColumns();
        }

        protected void cmdMoveLeftSum_Click(object sender, EventArgs e)
        {
            // Loop backwards through the list of SUM columns
            for (int i = (lstSum.Items.Count - 1); i >= 0; i--)
            {
                // If the current item is selected
                if (lstSum.Items[i].Selected == true)
                {
                    // Check the data type
                    switch (ReportDefinition.GetColumnDataType(lstSum.Items[i].Text))
                    {
                        // Numeric data types
                        case "System.Decimal":
                        case "System.Double":
                        case "System.Int16":
                        case "System.Int32":
                        case "System.Int64":
                        case "System.Single":
                        case "System.UInt16":
                        case "System.UInt32":
                        case "System.UInt64":
                            // Add the item to the available list
                            lstCols.Items.Add(lstSum.Items[i]);
                            // Remove the SUM column
                            lstSum.Items.Remove(lstSum.Items[i]);
                            // Stop processing after the first item
                            break;
                    }
                }
            }

            // Loop through the list of available columns
            for (int i = 0; i < lstCols.Items.Count; i++)
            {
                // Ensure the current item is not selected
                lstCols.Items[i].Selected = false;
            }

            UpdateAggregateColumns();
        }

        protected void cmdMoveRightAvg_Click(object sender, EventArgs e)
        {
            // Loop through the list of available columns
            for (int i = 0; i < lstCols.Items.Count; i++)
            {
                // If the current item is selected
                if (lstCols.Items[i].Selected == true)
                {
                    // Check the data type
                    switch (ReportDefinition.GetColumnDataType(lstCols.Items[i].Text))
                    {
                        // Numeric data types
                        case "System.Decimal":
                        case "System.Double":
                        case "System.Int16":
                        case "System.Int32":
                        case "System.Int64":
                        case "System.Single":
                        case "System.UInt16":
                        case "System.UInt32":
                        case "System.UInt64":
                            // Add the current item to AVG
                            lstAvg.Items.Add(lstCols.Items[i]);
                            // Stop processing after the first item
                            break;
                    }
                }
            }

            // Loop backwards through the list of available columns
            for (int i = (lstCols.Items.Count - 1); i >= 0; i--)
            {
                // If the current item is selected
                if (lstCols.Items[i].Selected == true)
                {
                    // Check the data type
                    switch (ReportDefinition.GetColumnDataType(lstCols.Items[i].Text))
                    {
                        // Numeric data types
                        case "System.Decimal":
                        case "System.Double":
                        case "System.Int16":
                        case "System.Int32":
                        case "System.Int64":
                        case "System.Single":
                        case "System.UInt16":
                        case "System.UInt32":
                        case "System.UInt64":
                            // Remove the current item from the available list
                            lstCols.Items.Remove(lstCols.Items[i]);
                            // Stop processing after the first item
                            break;
                    }
                }
            }

            // Loop through the list of AVG columns
            for (int i = 0; i < lstAvg.Items.Count; i++)
            {
                // Ensure the current item is not selected
                lstAvg.Items[i].Selected = false;
            }

            UpdateAggregateColumns();
        }

        protected void cmdMoveLeftAvg_Click(object sender, EventArgs e)
        {
            // Loop backwards through the list of AVG columns
            for (int i = (lstAvg.Items.Count - 1); i >= 0; i--)
            {
                // If the current item is selected
                if (lstAvg.Items[i].Selected == true)
                {
                    // Check the data type
                    switch (ReportDefinition.GetColumnDataType(lstAvg.Items[i].Text))
                    {
                        // Numeric data types
                        case "System.Decimal":
                        case "System.Double":
                        case "System.Int16":
                        case "System.Int32":
                        case "System.Int64":
                        case "System.Single":
                        case "System.UInt16":
                        case "System.UInt32":
                        case "System.UInt64":
                            // Add the item to the available list
                            lstCols.Items.Add(lstAvg.Items[i]);
                            // Remove the AVG column
                            lstAvg.Items.Remove(lstAvg.Items[i]);
                            // Stop processing after the first item
                            break;
                    }
                }
            }

            // Loop through the list of available columns
            for (int i = 0; i < lstCols.Items.Count; i++)
            {
                // Ensure the current item is not selected
                lstCols.Items[i].Selected = false;
            }

            UpdateAggregateColumns();
        }

        protected void cmdMoveRightMax_Click(object sender, EventArgs e)
        {
            // Loop through the list of available columns
            for (int i = 0; i < lstCols.Items.Count; i++)
            {
                // If the current item is selected
                if (lstCols.Items[i].Selected == true)
                {
                    // Check the data type
                    switch (ReportDefinition.GetColumnDataType(lstCols.Items[i].Text))
                    {
                        // Numeric data types
                        case "System.Decimal":
                        case "System.Double":
                        case "System.Int16":
                        case "System.Int32":
                        case "System.Int64":
                        case "System.Single":
                        case "System.UInt16":
                        case "System.UInt32":
                        case "System.UInt64":
                            // Add the current item to MAX
                            lstMax.Items.Add(lstCols.Items[i]);
                            // Stop processing after the first item
                            break;
                    }
                }
            }

            // Loop backwards through the list of available columns
            for (int i = (lstCols.Items.Count - 1); i >= 0; i--)
            {
                // If the current item is selected
                if (lstCols.Items[i].Selected == true)
                {
                    // Check the data type
                    switch (ReportDefinition.GetColumnDataType(lstCols.Items[i].Text))
                    {
                        // Numeric data types
                        case "System.Decimal":
                        case "System.Double":
                        case "System.Int16":
                        case "System.Int32":
                        case "System.Int64":
                        case "System.Single":
                        case "System.UInt16":
                        case "System.UInt32":
                        case "System.UInt64":
                            // Remove the current item from the available list
                            lstCols.Items.Remove(lstCols.Items[i]);
                            // Stop processing after the first item
                            break;
                    }
                }
            }

            // Loop through the list of MAX columns
            for (int i = 0; i < lstMax.Items.Count; i++)
            {
                // Ensure the current item is not selected
                lstMax.Items[i].Selected = false;
            }

            UpdateAggregateColumns();
        }

        protected void cmdMoveLeftMax_Click(object sender, EventArgs e)
        {
            // Loop backwards through the list of MAX columns
            for (int i = (lstMax.Items.Count - 1); i >= 0; i--)
            {
                // If the current item is selected
                if (lstMax.Items[i].Selected == true)
                {
                    // Check the data type
                    switch (ReportDefinition.GetColumnDataType(lstMax.Items[i].Text))
                    {
                        // Numeric data types
                        case "System.Decimal":
                        case "System.Double":
                        case "System.Int16":
                        case "System.Int32":
                        case "System.Int64":
                        case "System.Single":
                        case "System.UInt16":
                        case "System.UInt32":
                        case "System.UInt64":
                            // Add the item to the available list
                            lstCols.Items.Add(lstMax.Items[i]);
                            // Remove the MAX column
                            lstMax.Items.Remove(lstMax.Items[i]);
                            // Stop processing after the first item
                            break;
                    }
                }
            }

            // Loop through the list of available columns
            for (int i = 0; i < lstCols.Items.Count; i++)
            {
                // Ensure the current item is not selected
                lstCols.Items[i].Selected = false;
            }

            UpdateAggregateColumns();
        }

        protected void cmdMoveRightMin_Click(object sender, EventArgs e)
        {
            // Loop through the list of available columns
            for (int i = 0; i < lstCols.Items.Count; i++)
            {
                // If the current item is selected
                if (lstCols.Items[i].Selected == true)
                {
                    // Check the data type
                    switch (ReportDefinition.GetColumnDataType(lstCols.Items[i].Text))
                    {
                        // Numeric data types
                        case "System.Decimal":
                        case "System.Double":
                        case "System.Int16":
                        case "System.Int32":
                        case "System.Int64":
                        case "System.Single":
                        case "System.UInt16":
                        case "System.UInt32":
                        case "System.UInt64":
                            // Add the current item to MIN
                            lstMin.Items.Add(lstCols.Items[i]);
                            // Stop processing after the first item
                            break;
                    }
                }
            }

            // Loop backwards through the list of available columns
            for (int i = (lstCols.Items.Count - 1); i >= 0; i--)
            {
                // If the current item is selected
                if (lstCols.Items[i].Selected == true)
                {
                    // Check the data type
                    switch (ReportDefinition.GetColumnDataType(lstCols.Items[i].Text))
                    {
                        // Numeric data types
                        case "System.Decimal":
                        case "System.Double":
                        case "System.Int16":
                        case "System.Int32":
                        case "System.Int64":
                        case "System.Single":
                        case "System.UInt16":
                        case "System.UInt32":
                        case "System.UInt64":
                            // Remove the current item from the available list
                            lstCols.Items.Remove(lstCols.Items[i]);
                            // Stop processing after the first item
                            break;
                    }
                }
            }

            // Loop through the list of MIN columns
            for (int i = 0; i < lstMin.Items.Count; i++)
            {
                // Ensure the current item is not selected
                lstMin.Items[i].Selected = false;
            }

            UpdateAggregateColumns();
        }

        protected void cmdMoveLeftMin_Click(object sender, EventArgs e)
        {
            // Loop backwards through the list of MIN columns
            for (int i = (lstMin.Items.Count - 1); i >= 0; i--)
            {
                // If the current item is selected
                if (lstMin.Items[i].Selected == true)
                {
                    // Check the data type
                    switch (ReportDefinition.GetColumnDataType(lstMin.Items[i].Text))
                    {
                        // Numeric data types
                        case "System.Decimal":
                        case "System.Double":
                        case "System.Int16":
                        case "System.Int32":
                        case "System.Int64":
                        case "System.Single":
                        case "System.UInt16":
                        case "System.UInt32":
                        case "System.UInt64":
                            // Add the item to the available list
                            lstCols.Items.Add(lstMin.Items[i]);
                            // Remove the MIN column
                            lstMin.Items.Remove(lstMin.Items[i]);
                            // Stop processing after the first item
                            break;
                    }
                }
            }

            // Loop through the list of available columns
            for (int i = 0; i < lstCols.Items.Count; i++)
            {
                // Ensure the current item is not selected
                lstCols.Items[i].Selected = false;
            }

            UpdateAggregateColumns();
        }

        protected void cmdMoveRightCount_Click(object sender, EventArgs e)
        {
            // Loop through the list of available columns
            for (int i = 0; i < lstCols.Items.Count; i++)
            {
                // If the current item is selected
                if (lstCols.Items[i].Selected == true)
                {
                    // Check the data type
                    switch (ReportDefinition.GetColumnDataType(lstCols.Items[i].Text))
                    {
                        // Supported data types
                        case "System.Boolean":
                        case "System.Char":
                        case "System.String":
                        case "System.DateTime":
                        case "System.Decimal":
                        case "System.Double":
                        case "System.Int16":
                        case "System.Int32":
                        case "System.Int64":
                        case "System.Single":
                        case "System.UInt16":
                        case "System.UInt32":
                        case "System.UInt64":
                            // Add the current item to COUNT
                            lstCount.Items.Add(lstCols.Items[i]);
                            // Stop processing after the first item
                            break;
                    }
                }
            }

            // Loop backwards through the list of available columns
            for (int i = (lstCols.Items.Count - 1); i >= 0; i--)
            {
                // If the current item is selected
                if (lstCols.Items[i].Selected == true)
                {
                    // Check the data type
                    switch (ReportDefinition.GetColumnDataType(lstCols.Items[i].Text))
                    {
                        // Supported data types
                        case "System.Boolean":
                        case "System.Char":
                        case "System.String":
                        case "System.DateTime":
                        case "System.Decimal":
                        case "System.Double":
                        case "System.Int16":
                        case "System.Int32":
                        case "System.Int64":
                        case "System.Single":
                        case "System.UInt16":
                        case "System.UInt32":
                        case "System.UInt64":
                            // Remove the current item from the available list
                            lstCols.Items.Remove(lstCols.Items[i]);
                            // Stop processing after the first item
                            break;
                    }
                }
            }

            // Loop through the list of COUNT columns
            for (int i = 0; i < lstCount.Items.Count; i++)
            {
                // Ensure the current item is not selected
                lstCount.Items[i].Selected = false;
            }

            UpdateAggregateColumns();
        }

        protected void cmdMoveLeftCount_Click(object sender, EventArgs e)
        {
            // Loop backwards through the list of COUNT columns
            for (int i = (lstCount.Items.Count - 1); i >= 0; i--)
            {
                // If the current item is selected
                if (lstCount.Items[i].Selected == true)
                {
                    // Check the data type
                    switch (ReportDefinition.GetColumnDataType(lstCount.Items[i].Text))
                    {
                        // Supported data types
                        case "System.Boolean":
                        case "System.Char":
                        case "System.String":
                        case "System.DateTime":
                        case "System.Decimal":
                        case "System.Double":
                        case "System.Int16":
                        case "System.Int32":
                        case "System.Int64":
                        case "System.Single":
                        case "System.UInt16":
                        case "System.UInt32":
                        case "System.UInt64":
                            // Add the item to the available list
                            lstCols.Items.Add(lstCount.Items[i]);
                            // Remove the COUNT column
                            lstCount.Items.Remove(lstCount.Items[i]);
                            // Stop processing after the first item
                            break;
                    }
                }
            }

            // Loop through the list of available columns
            for (int i = 0; i < lstCols.Items.Count; i++)
            {
                // Ensure the current item is not selected
                lstCols.Items[i].Selected = false;
            }

            UpdateAggregateColumns();
        }
    }
}